# 二叉搜索树中第k小的元素


# 二叉搜索树的中序遍历为递增序列
# 题目可以转化为中序遍历的第k个节点

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def KthSmallmest(self, root, k) -> int:

        def dfs(root):

            if not root:
                return
            
            dfs(root.left)
            if self.k == 0:
                return
            self.k -= 1
            if self.k == 0:
                self.res = root.val
                return

            dfs(root.right)

        self.k = k
        dfs(root)

        return self.res




